[PHP] using the magic __set() method with 2D arrays

Posted by Spoonface on Stack Overflow See other posts from Stack Overflow or by Spoonface
Published on 2010-04-13T18:02:13Z Indexed on 2010/04/13 18:33 UTC
Read the original article Hit count: 272

If I have the following registry class:

Class registry 
{
    private $_vars;

    public function __construct()
    {
        $this->_vars = array();
    }

    public function __set($key, $val)
    {
        $this->_vars[$key] = $val;
    }

    public function __get($key)
    {
        if (isset($this->_vars[$key]))
            return $this->_vars[$key];
    }

    public function printAll()
    {
        print "<pre>".print_r($this->_vars,true)."</pre>";
    }
}

$reg = new registry();

$reg->arr = array(1,2,3);
$reg->arr = array_merge($reg->arr,array(4));

$reg->printAll();

Would there be an easier way to push a new item onto the 'arr' array? This code: 'array[] = item' doesn't work with the magic set method, and I couldn't find any useful info with google. Thanks for your time!

© Stack Overflow or respective owner

Related posts about php

Related posts about class